/*
 * @Copyright: 2025 Copyright (C) AMCAX, All rights reserved.
 * @Author: Zhang
 * @Date: 2025/7/24 19:39
 * @LastEditTime: 2025/7/24 19:39
 * @LastEditors: Zhang
 * @Description:
 */
import { TopoDialogProps, NMMeshBaseCommand } from '../type';

import { CmdDialogForm } from 'nextcae-base';
import { useEffect, useState } from 'react';
import { schema, uiSchema } from './data';
import { widgetManager } from '@/common/managers';
import { modelClean } from '@/domain';
import { useDocumentStore } from '@/common/store';

const MeshSimplifyDialog = ({ title, onClose }: TopoDialogProps) => {
  const [formData, setFormData] = useState({
    Options: {
      ratio: 0.5,
    },
  });
  // 获取原始网格id
  const { workingObject } = useDocumentStore();

  useEffect(() => {
    widgetManager.hint('进入网格简化命令!');
    return () => {
      widgetManager.hint();
    };
  }, []);

  const onSubmit = async () => {
    const params = {
      root: workingObject,
      ratio: formData.Options.ratio,
    };
    await modelClean.MeshSimplify(params);
    onClose();
  };
  return (
    <>
      <CmdDialogForm
        title={title}
        titleIcon={'commands/modelClean/mesh-simplify'}
        schema={schema}
        extraUiSchema={uiSchema}
        formData={formData}
        fields={{}}
        onChange={(data) => {
          setFormData(data.formData);
        }}
        onSubmit={onSubmit}
        onClose={onClose}
        isFormDataValid={true}
        width='300px'
      />
    </>
  );
};
export const cmdMeshSimplify: NMMeshBaseCommand = {
  id: 'cmdMeshSimplify',
  icon: 'commands/modelClean/mesh-simplify',
  label: '网格简化',
  create: (label: string, onClose: () => void, props) => (
    <MeshSimplifyDialog
      title={label}
      onClose={onClose}
      props={props}
      placement='topLeft'
    />
  ),
};
